package org.example.myleet.stringprocess;

public class StringProcessSolution {

    //65【字符串处理】有效数字
    public boolean isNumber(String s) {
        int ei = s.indexOf('e');
        if (ei < 0) {
            ei = s.indexOf('E');
        }
        if (s.indexOf('e', ei + 1) > -1) {
            return false;
        }
        if (s.indexOf('E', ei + 1) > -1) {
            return false;
        }
        if (s.indexOf('e') > -1) {
            //is exponent
            String[] nums = s.split("e");
            if (nums.length != 2) {
                return false;
            }
            return (isInteger(nums[0]) || isDouble(nums[0])) && isInteger(nums[1]);
        } else if (s.indexOf('E') > -1) {
            //is exponent
            String[] nums = s.split("E");
            if (nums.length != 2) {
                return false;
            }
            return (isInteger(nums[0]) || isDouble(nums[0])) && isInteger(nums[1]);
        } else {
            //not exponent, may be integer or double
            return isInteger(s) || isDouble(s);
        }
    }
    private boolean isInteger(String s) {
        String copy = s + "";
        if (copy.length() == 0) {
            return false;
        }
        char c = copy.charAt(0);
        if (c == '+' || c == '-') {
            copy = copy.substring(1);
            if (copy.length() == 0) {
                return false;
            }
        }
        if (copy.indexOf('+') > -1 || copy.indexOf('-') > -1 || copy.indexOf('.') > -1) {
            return false;
        }
        for (int i = 0; i < copy.length(); ++i) {
            c = copy.charAt(i);
            if ('0' <= c && c <= '9') {
            } else {
                return false;
            }
        }
        return true;
    }
    private boolean isDouble(String s) {
        String copy = s + "";
        if (copy.length() == 0) {
            return false;
        }
        char c = copy.charAt(0);
        if (c == '+' || c == '-') {
            copy = copy.substring(1);
            if (copy.length() == 0) {
                return false;
            }
        }
        if (copy.indexOf('+') > -1 || copy.indexOf('-') > -1 || copy.indexOf('.') < 0 || copy.indexOf('.', copy.indexOf('.') + 1) > -1) {
            return false;
        }
        String[] ints = copy.split("\\.");
        if (ints.length > 2) {
            return false;
        }
        boolean notEmpty = false;
        for(String is : ints) {
            if (is.length() > 0) {
                notEmpty = true;
                break;
            }
        }
        if (!notEmpty) {
            return false;
        }
        for(String is : ints) {
            for (int i = 0; i < is.length(); ++i) {
                c = is.charAt(i);
                if ('0' <= c && c <= '9') {
                } else {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 165【字符串分割+数字转化】比较版本号
     */
    public int compareVersion(String version1, String version2) {
        int i1 = 0, i2 = 0;
        String[] nums1 = version1.split("\\.");
        String[] nums2 = version2.split("\\.");
        while (i1 < nums1.length || i2 < nums2.length) {
            int num1;
            if (i1 >= nums1.length) {
                num1 = 0;
            } else {
                num1 = Integer.parseInt(nums1[i1]);
            }
            int num2;
            if (i2 >= nums2.length) {
                num2 = 0;
            } else {
                num2 = Integer.parseInt(nums2[i2]);
            }
            if (num1 < num2) {
                return -1;
            } else if (num1 > num2) {
                return 1;
            } else {
                if (i1 < nums1.length) {
                    ++i1;
                }
                if (i2 < nums2.length) {
                    ++i2;
                }
            }
        }
        return 0;
    }
}
